﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace DcsGauges.Shared.DataStructures
{
    public static class DcsDevices
    {
        private static readonly Dictionary<int, List<int>>              deviceIdToPropertyId                = null;
        private static readonly Dictionary<int, int>                    propertyIdToDeviceId                = null;

        static DcsDevices()
        {
            //extract device id's from devices.lua
            deviceIdToPropertyId = new Dictionary<int,List<int>>()
            {
                //electrical panel (id = 1)
                { 1, new List<int>() 
                    {
                        DcsProperties.ElectricalPanel.AcPowerGen.Left,
                        DcsProperties.ElectricalPanel.AcPowerGen.Right,
                        DcsProperties.ElectricalPanel.ApuGenerator,
                        DcsProperties.ElectricalPanel.BatteryPower,
                        DcsProperties.ElectricalPanel.EmergencyFlood,
                        DcsProperties.ElectricalPanel.Inverter,
                    }
                },
            };



            //created reversed dictionary property -> deviceId
            propertyIdToDeviceId        = new Dictionary<int, int>();
            var newKeys                 = deviceIdToPropertyId.Values.SelectMany(v => v).Distinct();

            foreach (var nk in newKeys)
            {
                var vals                = deviceIdToPropertyId.Keys.Where(k => deviceIdToPropertyId[k].Contains(nk));
                Debug.Assert(vals.Count() == 1); //when every property is unique there is only one element, otherwise some property was sepcified twice
                propertyIdToDeviceId.Add(nk, vals.First());
            }
        }

        public static int GetDeviceId(DcsProperty property)
        {
            return propertyIdToDeviceId[property];
        }
    }
}
